import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateRbacUserDto } from './dto/create-rbac_user.dto';
import { UpdateRbacUserDto } from './dto/update-rbac_user.dto';
import { EntityManager, In } from 'typeorm';
import { InjectEntityManager } from '@nestjs/typeorm';
import { RbacUser } from './entities/rbac_user.entity';
import { RbacRole } from './entities/rbac_role.entity';
import { RbacPermission } from './entities/rbac_permission.entity';
import { UserLoginDto } from './dto/user-login.dto';

@Injectable()
export class RbacUserService {

  @InjectEntityManager()
  entityManager: EntityManager

  async initData() {
    const user1 = new RbacUser();
    user1.username = '张三';
    user1.password = '111111';

    const user2 = new RbacUser();
    user2.username = '李四';
    user2.password = '222222';

    const user3 = new RbacUser();
    user3.username = '王五';
    user3.password = '333333';

    const role1 = new RbacRole();
    role1.name = '管理员';

    const role2 = new RbacRole();
    role2.name = '普通用户';

    const permission1 = new RbacPermission();
    permission1.name = '新增 aaa';

    const permission2 = new RbacPermission();
    permission2.name = '修改 aaa';

    const permission3 = new RbacPermission();
    permission3.name = '删除 aaa';

    const permission4 = new RbacPermission();
    permission4.name = '查询 aaa';

    const permission5 = new RbacPermission();
    permission5.name = '新增 bbb';

    const permission6 = new RbacPermission();
    permission6.name = '修改 bbb';

    const permission7 = new RbacPermission();
    permission7.name = '删除 bbb';

    const permission8 = new RbacPermission();
    permission8.name = '查询 bbb';


    role1.permissions = [
      permission1,
      permission2,
      permission3,
      permission4,
      permission5,
      permission6,
      permission7,
      permission8
    ]

    role2.permissions = [
      permission1,
      permission2,
      permission3,
      permission4
    ]

    user1.roles = [role1];

    user2.roles = [role2];

    await this.entityManager.save(RbacPermission, [
      permission1, 
      permission2,
      permission3,
      permission4,
      permission5,
      permission6,
      permission7,
      permission8
    ])

    await this.entityManager.save(RbacRole, [
      role1,
      role2
    ])

    await this.entityManager.save(RbacUser, [
      user1,
      user2
    ])  
  }

  async loginByJwt(userLogin: UserLoginDto) {
    const user = await this.entityManager.findOne(RbacUser, {
      where: {
        username: userLogin.username
      },
      relations: {
        roles: true
      }
    })
    if (!user) {
      throw new HttpException('用户不存在', HttpStatus.ACCEPTED)
    }
    if (user.password !== userLogin.password) {
      throw new HttpException('密码错误', HttpStatus.ACCEPTED)
    }
    return user
  }

  async findRolesByIds(roeIds: number[]) {
    return this.entityManager.find(RbacRole, {
      where: {
        id: In(roeIds)
      },
      relations: {
        permissions: true
      }
    })
  }
}
